namespace AltNetUK.Controllers
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Web;
    using System.Web.Mvc;
    using SimpleWebServices;
    using SimpleWebServices.Configuration;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Mvc;
    using AltNetUK.Models;

    [InternalServerError]
    [Allow(Methods = "GET, HEAD, POST, OPTIONS")]
    [CacheControl, ContentLanguage, ContentMD5]
    public sealed class RegistrationController : Controller
    {
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This needs to be an instance member.")]
        public ActionResult LanguageNegotiation(string unique)
        {
            string location = string.Format(
                CultureInfo.InvariantCulture,
                "/registration.en{0}",
                string.IsNullOrEmpty(unique) ? string.Empty : string.Concat("/", unique));

            return new SeeOtherResult(location);
        }

        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This needs to be an instance member.")]
        public ActionResult ContentNegotiation(string unique, CultureInfo language)
        {
            if (null == language)
            {
                throw new ArgumentNullException("language");
            }
            else if (CultureInfo.InvariantCulture == language)
            {
                throw new ArgumentOutOfRangeException("language");
            }

            string location = string.Format(
                CultureInfo.InvariantCulture,
                "/registration.{0}.html{1}",
                language,
                string.IsNullOrEmpty(unique) ? string.Empty : string.Concat("/", unique));

            return new SeeOtherResult(location);
        }

        public ActionResult HtmlRepresentation(string unique, CultureInfo language)
        {
            if (null == language)
            {
                throw new ArgumentNullException("language");
            }
            else if (CultureInfo.InvariantCulture == language)
            {
                throw new ArgumentOutOfRangeException("language");
            }

            ActionResult result = null;

            RegistrationForm model = RegistrationForm.Load(unique);
            if (!string.IsNullOrEmpty(unique) && !(model.Data as IPersistable).Unique.HasValue)
            {
                result = new SeeOtherResult("/registration");
            }
            else
            {
                this.Response.StatusCode = (int)model.Status;
                result = View(model);
            }

            return result;
        }

        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This needs to be an instance member.")]
        public ActionResult Post(RegistrationForm model)
        {
            if (null == model)
            {
                throw new ArgumentNullException("model");
            }

            model.Store();

            return new SeeOtherResult("/registration/" + (model.Data as IPersistable).Unique.Value);
        }
    }
}